package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Saleorder;
import cn.wolfcode.crm.service.ISaleorderService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.JSONUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Controller
@RequestMapping(value = {"cashier","saleAnalysis"})
public class CashierController {

    @Autowired
    private ISaleorderService saleorderService;


    @RequestMapping("view")
    public String view() {
        return "cashier/list";
    }


    @RequestMapping("carouselView")
    public String carouselView() {
        return "carousel/view";
    }


    @RequestMapping("saleAnalyzeView")
    public String saleAnalysisView() {
        return "saleAnalyze/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object sale(Saleorder saleorder) {

        JSONResult result = JSONResult.success("保存成功");

        try {
            saleorderService.saveOrUpdate(saleorder);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return result;
    }


    @RequestMapping("audit")
    @ResponseBody
    public Object audit(Long id) {
        JSONResult result = JSONResult.success("审核成功");
        try {
            if (Optional.ofNullable(id).isPresent()) {
                saleorderService.audit(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return result;
    }




    @RequestMapping("saleAnalyzeList")
    @ResponseBody
    public Object saleAnalyzeList() {

        List<HashMap<String,Object>> list = saleorderService.selectSaleAnalyzeList();

        return list;
    }


    @RequestMapping("saleByBar")
    @ResponseBody
    public Object saleByBar() {

        List<HashMap<String,Object>> list = saleorderService.selectSaleAnalyzeList();
        ArrayList<Object> x = new ArrayList<>();
        ArrayList<Object> y = new ArrayList<>();
        for (Map<String, Object> map : list) {
            x.add(map.get("pName"));
            y.add(map.get("amount"));
        }

        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("x",x);
        resultMap.put("y",y);

        return resultMap;
    }



    @RequestMapping("saleByPie")
    @ResponseBody
    public Object saleByPie() {

        List<HashMap<String,Object>> list = saleorderService.selectSaleAnalyzeList();
        ArrayList<Object> x = new ArrayList<>();
        ArrayList<Map<Object, Object>> datas = new ArrayList<>();
        for (Map<String, Object> map : list) {
            x.add(map.get("pName"));

            Map<Object, Object> data = new HashMap<>();
            data.put("value", map.get("amount"));
            data.put("name", map.get("pName"));
            datas.add(data);
        }


        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("x", x);
        resultMap.put("datas", datas);

        return resultMap;
    }



    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response) throws IOException {
        //设置下载的响应头信息
        response.addHeader("Content-disposition","attachment;filename=saleChart.xls");
        //1.查询所有的员工信息
        List<HashMap<String, Object>> list = saleorderService.selectSaleAnalyzeList();

        // 2.创建一个exel工作表 需要到poi的包
        HSSFWorkbook workbook = new HSSFWorkbook();
        //3.创建一个工作页签
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow header = sheet.createRow(0);
        //4.创建单元格信息
        header.createCell(0).setCellValue("产品编码");
        header.createCell(1).setCellValue("商品名");
        header.createCell(2).setCellValue("销售数量");
        header.createCell(3).setCellValue("销售总金额");
        header.createCell(4).setCellValue("毛利");
        for (int i = 0; i < list.size(); i ++){
            HashMap<String, Object> map = list.get(i);
            HSSFRow currentRow = sheet.createRow(i + 1);

            //5.创建单元格信息
            currentRow.createCell(0).setCellValue(map.get("pSn").toString());
            currentRow.createCell(1).setCellValue(map.get("pName").toString());
            currentRow.createCell(2).setCellValue(map.get("number").toString());
            currentRow.createCell(3).setCellValue(map.get("amount").toString());
            currentRow.createCell(4).setCellValue(map.get("maori").toString());

        }

        workbook.write(response.getOutputStream());
        //关闭流操作
        workbook.close();
    }





}
